<%= content_for :javascript do %>
  <script src="/javascripts/app/documents.js"></script>
  <script type="text/javascript">
    
    var addDocumentPopup = null;
    
    $(document).ready(function () {
      
      <% if @current_account.admin? %>
        $(".save-document").click(function (e) {
          e.preventDefault();
          $("#add-document-frame").contents().find("#new_document").submit();
        });
        
        $(".save-document-permissions").click(function (e) {
          e.preventDefault();
          $("#view-document-permissions-frame").contents().find(".form-horizontal").submit();
        });         
      <% end %>
      
      <% if @has_active_categories %>
        $(".nav-list a").click(function (e) {
          e.preventDefault();
          $(".nav-list li").removeClass("active");
          $(this).parent().addClass("active");
          var categoryId = $(this).attr("data-id");
          $(".search-query").attr("data-id",categoryId);
          App.DocumentController.init({"category": categoryId});
        });

        App.DocumentController = new DocumentController({"category": <%= @active_category_id %>});        
        
        $(".search-query").attr("data-id",<%= @active_category_id %>);
        
      
        $(".search-query").keypress(function(e) {            
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) {
              App.DocumentController.search({
                "category": $(this).attr("data-id"),
                "search_query": $(this).val()
              });
            }
        });        
        
      <% end %>      
    });  
    
    <% if @current_account.admin? %>
      function addDocument(categoryId) {
        $("#add-document-frame").contents().find("#document_category_id").val(categoryId);
        addDocumentPopup = $("#add-document").modal("show");      
      }
            
      function deleteDocument(documentId) {
        App.DocumentController.deleteDocument(documentId);
      } 
      
      function viewPermissions(documentId,categoryId) {
          src = "/documents/permissions/"+documentId;
          $("#view-document-permissions-frame").attr("src",src);
          viewPermissionsPopup = $("#view-document-permissions").modal("show");
      }        
    <% end %>
  </script>
<% end %>
    <div class="page-header">
      <h1 style="float:left;">Mociones</h1>
      <% if @has_active_categories %>
          <div class="pull-right input-append" style="padding-top:30px;">
            <input type="text" placeholder="Buscar Informes por nombre..." class="input-xlarge search-query">          
            </input>   
          </div>
      <% end %>
      <div class="clearfix"></div>      
    </div>
    <%= "No tiene permisos para acceder a esta sección" unless @has_active_categories %>
    <% if @has_active_categories %>
        <div class="row">
          <div class="span3">
              <div class="well" style="padding: 8px 0;">
                <ul class="nav nav-list">
                  <% @categories.each do |category| %>                
                      <li class="<%=  @active_category_id == category.id ? 'active' : ''%> <%= category.category_type == 'title' || category.category_type == 'standard-link' ? 'nav-header' : '' %>">
                        <% if category.category_type == 'standard' || category.category_type == 'standard-link' %>
                          <a href="#" data-id="<%= category.id %>"><%= category.name %></a>
                        <% else %>
                          <%= category.name %>
                        <% end %>
                      </li>
                      <li class="divider"></li>                
                  <% end %>
                </ul>      
              </div>
          </div>
          <div class="span9">
            <div id="section-container">
            </div>
            <div class="documents-pagination pagination pagination-centered">    
            </div>        
          </div>
        </div>
    <% end %>
<%= content_for :modal do %>
  <% if @current_account.admin? %>
    <div class="modal hide" id="add-document" style="height:320px;">
        <div>
          <!-- ** widget header ** -->
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4>Agregar documento</h4>
          </div>
          <!-- ** ./ widget header ** -->
          <!-- ** dummy content ** -->
          <iframe id="add-document-frame" src="/documents/new" style="width:100%;border:0px;height:220px;"></iframe>
          <!-- ** ./ dummy content ** -->
        </div>
        <div class="modal-footer">
          <a href="#" class="btn btn-primary save-document">Cargar documento</a> <a href="#" class="btn btn-warning close-owner" data-dismiss="modal">Cerrar</a>
        </div>
    </div>
    <div class="modal hide" id="view-document-permissions" style="height:320px;">
      <div>
        <!-- ** widget header ** -->
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4>Permisos del archivo</h4>
        </div>
        <!-- ** ./ widget header ** -->
        <!-- ** dummy content ** -->
        <iframe id="view-document-permissions-frame" src="" style="width:100%;border:0px;height:220px;"></iframe>
        <!-- ** ./ dummy content ** -->
      </div>
      <div class="modal-footer">
        <a href="#" class="btn btn-primary save-document-permissions">Grabar permisos</a> <a href="#" class="btn btn-warning close-document-permissions" data-dismiss="modal">Cerrar</a>
      </div>
    </div>  
  <% end %>
<% end %>
  
<%= content_for :templates do %>
  <% if @has_active_categories %>
    <script id="documents-template" type="text/x-handlebars-template">
      <section style="padding-top: 0px;">
        <h3>
          <span>{{category_name}}</span>
          <div class="pull-right">
            <% if @current_account.admin? %>
              <a href="#" class="btn btn-primary btn-small add-document" onclick="addDocument({{category_id}})">Añadir documento</a>
            <% end %>
          </div>
        </h3>      
        <div class="clearfix">
            {{#if documents}}
            <table class="table table-bordered table-hover">
              <tr>
                <th>Fecha</th>
                <th>Nombre</th>
                <th>Descripción</th>
                <th>Acciones</th>
              </tr>
              {{#each_with_index documents}}
                {{#with this}}
                <tr>
                  <td>{{#formatDate upload_at}}
                        j f Y
                      {{/formatDate}}
                  </td>
                  <td>{{name}}</td>
                  <td>{{description}}</td>
                  <td>
                    <a href="{{content_url}}">Descargar</a>
                    <% if @current_account.admin? %>
                      | <a href="#" onclick="deleteDocument({{id}})">Eliminar</a>
                      | <a href="#" onclick="viewPermissions({{id}})">Permisos</a>
                    <% end %>
                  </td>
                </tr>
              {{/with}}
            {{/each_with_index}}                          
            </table>
            {{/if}}
            {{#unless documents}}No hay documentos que mostrar aun.{{/unless}}
        </div>
      </section>    
    </script>   
    <script id="pagination" type="text/x-handlebars-template">
      <ul>{{{create_pagination_links currentPage perPage totalEntries paginateFunction category_id}}}</ul>
    </script>  
  <% end %>
<% end %>
